#include "Camera.h"

class Matrix;

Matrix Camera::LookAt(Vector3 target)
{
	this->target = target;
	viewProjectionMatrix.SetLookAt(position, target, up);
	return viewProjectionMatrix * mPerpestive;
}

void Camera::Go(float up, float forward, float right)
{
	Vector4 MoveL = Vector4(right, up, forward,1.0) * m_speed;
	Vector4 MoveW = MoveL * worldMatrix;
	position = Vector3(MoveW.x, MoveW.y, MoveW.z);
}

void Camera::RotateUp(float angle)
{
	rotx += angle;
}

void Camera::RotateDown(float angle)
{
	rotx -= angle;
}

void Camera::RotateLeft(float angle)
{
	roty += angle;
}

void Camera::RotateRight(float angle)
{
	roty -= angle;
}

Matrix Camera::CalculateWorldMatrix()
{
	Matrix mRot;
	Matrix mTrans;
	Matrix mPerpestive;
	mRot.SetRotation(rotx, roty, rotz);
	mTrans.SetTranslation(position.x, position.y, position.z);
	worldMatrix = mRot * mTrans ;
	return worldMatrix;
}

Matrix Camera::CalculateViewMatrix()
{
	return CalculateWorldMatrix().Inverse() * mPerpestive;
}

void Camera::Init(float nearPlane, float farPlane, float FOV, float aspect, float speed)
{
	m_nearPlane = nearPlane;
	m_farPlane = farPlane;
	m_FOV = FOV;
	m_aspect = aspect;
	m_speed = speed;
	mPerpestive.SetPerspective(m_FOV, m_aspect, m_nearPlane, m_farPlane);
}

void Camera::Update()
{
	//87 65 83 68
	if (IM->IsKeyDown(87))
	{
		RotateUp(1.0 * Timer::FrameTime());
	}
	if (IM->IsKeyDown(83))
	{
		RotateDown(1.0 * Timer::FrameTime());
	}
	if (IM->IsKeyDown(65))
	{
		RotateLeft(1.0 * Timer::FrameTime());
	}
	if (IM->IsKeyDown(68))
	{
		RotateRight(1.0 * Timer::FrameTime());
	}
	if (IM->IsKeyDown(39))
	{
		Go(0, 0, 1.0 * Timer::FrameTime());
	}
	if (IM->IsKeyDown(37))
	{
		Go(0, 0, -1.0 * Timer::FrameTime());
	}
	if (IM->IsKeyDown(38))
	{
		Go(0, -1.0 * Timer::FrameTime(), 0);
	}
	if (IM->IsKeyDown(40))
	{
		Go(0, 1.0 * Timer::FrameTime(), 0);
	}
	if (IM->IsKeyDown(90))
	{
		Go(-1.0 * Timer::FrameTime(), 0, 0);
	}
	if (IM->IsKeyDown(88))
	{
		Go(1.0 * Timer::FrameTime(), 0, 0);
	}

}

Camera::Camera() 
{
	rotx = 0;
	roty = 0;
	rotz = 0;
	position = Vector3(0, 0, 0);
	IM = InputManager::GetInstance();
	worldMatrix.SetIdentity();
	mPerpestive.SetIdentity();
}

Camera::~Camera()
{
}
